/**
 * 支付相关服务
 */

const util = require('../utils/util.js');
const api = require('../config/api.js');
var app = getApp();

/**
 * 判断用户是否登录
 */
function payOrder(orderId) {
  return new Promise(function (resolve, reject) {
    util.request(api.PayPrepayId, {
      orderId: orderId
    }).then((res) => {
      if (res.errno === 0) {
        const payParam = res.data;

        //模板消息参数
        var self = this;
        //待付款提醒
          let data1={
            "keyword1": {
              "value": payParam.orderInfo.add_time,
              "color": "#173177"
            },
            "keyword2": {
              "value": payParam.body,
              "color": "#173177"
            },
            "keyword3": {
              "value": "待支付",
              "color": "#173177"
            },
            "keyword4": {
              "value": payParam.orderInfo.order_price,
              "color": "#173177"
            },
            "keyword5": {
              "value": "请在一个小时内完成支付",
              "color": "#173177"
            }
          };

        util.request(api.sendTemplateMsg, {
          openId: app.globalData.openId, formId: app.globalData.formId, templateId: 'zJlNJh_vJmwVgHjhv8V3fSuNoTpgxTottYzMnkkd5to', data: data1
        },"POST", 'application/json').then(function (res) {
          if (res.errno === 0) {
            console.log("success");
          }
          else {
            console.log("fail");
          }
        })
      


        wx.requestPayment({
          'timeStamp': payParam.timeStamp,
          'nonceStr': payParam.nonceStr,
          'package': payParam.package,
          'signType': payParam.signType,
          'paySign': payParam.paySign,
          'success': function (res) {
            resolve(res);
             //发送模板消息
            let data2={
            "keyword1": {
            "value": payParam.orderInfo.order_sn,
            "color": "#173177"
            },
            "keyword2": {
            "value": payParam.body,
            "color": "#173177"
            },
            "keyword3": {
            "value": payParam.orderInfo.add_time,
            "color": "#173177"
            },
           "keyword4": {
           "value": payParam.orderInfo.order_price,
           "color": "#173177"
           },
           "keyword5": {
           "value": payParam.orderInfo.full_region,
           "color": "#173177"
           }
        };

          util.request(api.sendTemplateMsg, {
            openId: app.globalData.openId, formId: app.globalData.formId, templateId: 'kc-XnjwLydSo_9k4ZdyBZukdIbBqTv6uD0dCLOMJYMI', data: data2
          }, "POST", 'application/json').then(function (res) {
            if (res.errno === 0) {
              console.log("success");
            }
            else {
              console.log("fail");
            }
          });
          

    //发送短信
            util.request(api.PaySuccess, { orderSn: payParam.orderInfo.order_sn, phone: payParam.orderInfo.mobile }, "POST", 'application/json')
              .then(function (res) {
                console.log(res);
              });

            util.request(api.OrderQuery, { orderId: payParam.orderInfo.id }).then(function (res) { })
          
        },
          'fail': function (res) {
            reject(res);
            util.request(api.OrderQuery, { orderId: payParam.orderInfo.id }).then(function (res) { })
          },
          'complete': function (res) {
            reject(res);
          }
        });
      } else {
        reject(res);
      }
    });
  });
}


module.exports = {
  payOrder,
};











